package cn.com.infcn.monitor.task.jvm.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class MBeanServerConnectionFactory {
	
	private static Map<String, MBeanServerConnection> mbsMap = new HashMap<String, MBeanServerConnection>();
	
	private MBeanServerConnectionFactory(){
	}
	
	public static MBeanServerConnection getInstance(String jmxURL) throws IOException {
		MBeanServerConnection mbs = mbsMap.get(jmxURL);
		if(mbs==null){
			JMXServiceURL address = new JMXServiceURL(jmxURL);
			JMXConnector connector = JMXConnectorFactory.connect(address);
			mbs = connector.getMBeanServerConnection();
			mbsMap.put(jmxURL, mbs);
			return mbs;
		}
		return mbs;
	}
}
